package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

@RestController
@Slf4j
@RequestMapping("/admin/dish")
public class DishController {
    @Autowired
    public DishService dishService;
    @Autowired
    RedisTemplate redisTemplate;

    @PostMapping
    @CacheEvict(value = "DishCache",key = "dishDTO.categoryId")
    public Result<String> save(@RequestBody DishDTO dishDTO){
        log.info("新增菜品");
        dishService.save(dishDTO);

//        //删除redis中的缓存
//        cleanCache("dishList_"+dishDTO.getCategoryId());
        return Result.success();
    }

    @DeleteMapping
    @CacheEvict(value = {"DishCache","SetmealCache","Setmeal_Dish"},allEntries = true)
    public Result<String> delete(@RequestParam("ids") Integer[] ids){
        log.info("删除菜品");
        List<Integer> idlist = Arrays.stream(ids).toList();
//        for (Integer a : ids){
//            //删除redis中的缓存
//            Result<DishVO> byId = getById(a.longValue());
//            Long id = byId.getData().getCategoryId();
//            //删除redis中的缓存
//            cleanCache("dishList_"+id);
//        }
        dishService.deleteById(idlist);


        return Result.success();
    }

    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("分页查询菜品");
        PageResult result = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(result);
    }

    @GetMapping("/list")
    public Result<List<Dish>> list(Integer categoryId){
        log.info("根据分类id查询菜品");
        List<Dish> list = dishService.selectByCategoryId(categoryId);
        return Result.success(list);
    }

    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable Long id){
        log.info("根据id查询菜品");
        DishVO dishVO = dishService.getById(id);
        return Result.success(dishVO);
    }

    @PutMapping
    @CacheEvict(value = {"DishCache","SetmealCache","Setmeal_Dish"},allEntries = true)
    public Result<String> update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品");
        dishService.update(dishDTO);
//        //删除redis中的缓存
//        cleanCache("dishList_*");
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @CacheEvict(value = {"DishCache","SetmealCache","Setmeal_Dish"},allEntries = true)
    public Result<String> startOrStop(@PathVariable Integer status,Long id){
        log.info("起售停售菜品");
//        //删除redis中所有缓存
//        cleanCache("dishList_*");
        dishService.startOrStop(status,id);
        return Result.success();
    }

}
